// get vars from url
$.extend({
	getUrlVars: function(){
		var vars = [], hash;
		var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
		for(var i = 0; i < hashes.length; i++) {
			hash = hashes[i].split('=');
			vars.push(hash[0]);
			vars[hash[0]] = hash[1];
		}
		return vars;
	},
	getUrlVar: function(name){
		return $.getUrlVars()[name];
	}
});

var x=document.getElementById("demo");
function getLocation()
{
	if (navigator.geolocation)
	{
		navigator.geolocation.getCurrentPosition(showPosition,showError);
	}
	else{x.innerHTML="Geolocation is not supported by this browser.";}
}

function showPosition(position)
{
	var latlon=position.coords.latitude+","+position.coords.longitude;

	var img_url="http://maps.googleapis.com/maps/api/staticmap?center="
		+latlon+"&zoom=14&size=440x235&sensor=false&maptype=roadmap&markers=color:red%7Clabel:A%7C"+latlon;
	document.getElementById("mapholder").innerHTML="<img src='"+img_url+"' class='bild'>";
	$('.button123').hide();
	$("#vimeo06-2").show();
	$('.bild').click(function() {
		window.open('http://maps.apple.com/?q='+latlon);
	});
}

var supportsInlineSvg = (function() {
	var div = document.createElement('div');
	div.innerHTML = '<svg/>';
	return (div.firstChild && div.firstChild.namespaceURI) == 'http://www.w3.org/2000/svg';
}());

var supportsInputRange = (function() {
	var input = document.createElement('input');
	input.setAttribute('type', 'range');
	return input.type != 'text';
}());

document.documentElement.className += (supportsInlineSvg ? ' inline-svg' : '') + (supportsInputRange ? ' input-range' : '');

function showError(error)
{
	switch(error.code)
	{
		case error.PERMISSION_DENIED:
			x.innerHTML="User denied the request for Geolocation."
			break;
		case error.POSITION_UNAVAILABLE:
			x.innerHTML="Location information is unavailable."
			break;
		case error.TIMEOUT:
			x.innerHTML="The request to get user location timed out."
			break;
		case error.UNKNOWN_ERROR:
			x.innerHTML="An unknown error occurred."
			break;
	}
}

/* Turn.js */
$(window).ready(function() {

	var windowHeight = $(window).height()
	$("#diary").css("height", windowHeight);

	var windoWidth = $(window).width()
	$("#diary").css("width", windoWidth);

	if (!supportsInlineSvg) return;
	var code = document.querySelectorAll('.codehilite');
	var button = document.querySelector('.squiggle-go');
	code = code[code.length-1].textContent;

	button.style.display = 'inline';
	button.onclick = function(event) {
		// muhahaha
		eval(code);
		event.preventDefault();
	};
	function generateCanvas(event) {
		// muhahaha
		eval(code);
		event.preventDefault();
	};

	$('#diary').turn({
		display: 'single',
		elevation: 50
	});

	/* HIER DIE BLÄTTER-SCHEISSE */
	$("#diary").bind("start", function(event, pageObject, corner) {
				//alert(pageObject);
		if (pageObject === 0) {
			//		alert('lol');
			//e.preventDefault();
			//return;
		}
	});
	$('.menuSlideFromTop').click(function() {
		if($(this).hasClass("active")){
			$(this).animate({ "top": "-988px" }, "slow" );
			$(this).removeClass("active");
			$(".lightbox").remove();
		} else {
			$(this).animate({ "top": "-103px" }, "slow" );
			$(this).addClass("active");
			$(this).after("<div class='lightbox'></div>");
			generateCanvas(event);
		}
	});
	/* jQuery Custom Scrollbar */
	$(".p2").mCustomScrollbar({
		scrollInertia: 300,
		autoDraggerLength:false
	});
	if ($.getUrlVar("element") != null) {
		//show the next container
		$('#diary').turn('page', 2);
		$("#vimeo05").show();
		$("#vimeo06-1").show();
	}
	$("#btnSubmit").click(function() {
		if($('#pwd').val() == 'Atlas') {
			$('#mapElement-bahn').show();
			alert("Super, danke dir für die Hilfe. Es geht weiter....");
			$("div.mCSB_container").css("top", "0");
			$("div.mCSB_dragger").css("top", "10px");
			var hbfElement = $(".hbf");
			$(".hbf").remove();
			$(".tasks").append(hbfElement);
			$(".hbf").show();
			$(".hbf").wrapInner("<s></s>");
		}
		//event.preventDefault();
		return false;
	});
});